package cc.ufinity.networkdiagnose.chart;

import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

/**
 * 线性图形报表类
 * 
 * @author ZR
 * 
 */
public class ChartManager {
	Context context;
	int xMax;
	int yMax;

	public ChartManager(Context context, final int xMax, final int yMax) {
		this.context = context;
		this.xMax = xMax;
		this.yMax = yMax;
	}

	public View createLineChart(final int[] colors, final String chartTitle,
			String[] lineTitles, List<double[]> x, List<double[]> y) {
		LineChart lineChart = new LineChart(context);
		// int[] colors = new int[] { Color.BLUE };
		int len = colors.length;
		PointStyle[] styles = new PointStyle[len];
		for (int i = 0; i < len; i++) {
			styles[i] = PointStyle.CIRCLE;
		}
		// PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE};
		return lineChart.create(colors, styles, chartTitle, lineTitles, x, y);
	}

	final class LineChart {
		private Context ctx;

		public LineChart(Context ctx) {
			this.ctx = ctx;
		}

		public View create(final int[] colors, final PointStyle[] styles,
				final String chartTitle, String[] lineTitles, List<double[]> x,
				List<double[]> y) {
			// lineTitles = new String[] { "First", "Second" };
			// x.add(new double[] { 1, 3, 5, 7, 9, 11 });
			// x.add(new double[] { 0, 2, 4, 6, 8, 10 });
			// y.add(new double[] { 3, 14, 5, 30, 20, 25 });
			// y.add(new double[] { 18, 9, 21, 15, 10, 6 });

			XYMultipleSeriesDataset dataset = buildDataset(lineTitles, x, y);
			XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles,
					true);
			setChartSettings(renderer, chartTitle, "X", "Y", 0, xMax, 0, yMax,
					Color.WHITE, Color.WHITE);
			View chart = ChartFactory.getLineChartView(ctx, dataset, renderer);
			 
			return chart;
		}

		protected XYMultipleSeriesDataset buildDataset(String[] titles,
				List<double[]> xValues, List<double[]> yValues) {
			XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
			int length = titles.length; // 有几条线
			for (int i = 0; i < length; i++) {
				XYSeries series = new XYSeries(titles[i]); // 根据每条线的名称创建
				double[] xV = xValues.get(i); // 获取第i条线的数据
				double[] yV = yValues.get(i);
				int seriesLength = xV.length; // 有几个点
				for (int k = 0; k < seriesLength; k++) // 每条线里有几个点
				{
					series.add(xV[k], yV[k]);
				}
				dataset.addSeries(series);
				 
			}
		 
			return dataset;
		}

		protected XYMultipleSeriesRenderer buildRenderer(int[] colors,
				PointStyle[] styles, boolean fill) {
			XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
			int length = colors.length;
			for (int i = 0; i < length; i++) {
				XYSeriesRenderer r = new XYSeriesRenderer();
				r.setColor(colors[i]);
				r.setPointStyle(styles[i]);
				r.setFillPoints(fill);
				renderer.addSeriesRenderer(r);
			}
			renderer.setMarginsColor(Color.argb(80, 00, 00, 00));
			return renderer;
		}

		protected void setChartSettings(XYMultipleSeriesRenderer renderer,
				String title, String xTitle, String yTitle, double xMin,
				double xMax, double yMin, double yMax, int axesColor,
				int labelsColor) {
			renderer.setChartTitle(title);
			renderer.setXTitle(xTitle);
			renderer.setYTitle(yTitle);
			renderer.setXAxisMin(xMin);
			renderer.setXAxisMax(xMax);
			renderer.setYAxisMin(yMin);
			renderer.setYAxisMax(yMax);
			renderer.setAxesColor(axesColor);
			renderer.setLabelsColor(labelsColor);
			renderer.setPanEnabled(false);
			renderer.setBackgroundColor(Color.argb(80, 00, 00, 00));
			renderer.setApplyBackgroundColor(true);
			renderer.setInScroll(true);
			renderer.setShowGrid(true);
			//renderer.setShowGridX(true);
			//renderer.setShowGridY(true);
			// renderer.setZoomEnabled(false);
		}
	}

}
